//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.transaction.xaframework
{

	using XAException = javax.transaction.xa.XAException;
	using XAResource = javax.transaction.xa.XAResource;

///
/// <summary> * <CODE>XaConnectionHelpImpl</CODE> helps with basic implementation that is
/// * needed to fit a non XA compatible resource in the <CODE>xaframework</CODE>.
/// * <p>
/// * Put your methods that perform any transactional work towards the resource in
/// * your extention of this class. Create a instance of the
/// * <seealso cref="XaResourceHelpImpl"/> as your <CODE>XAResource</CODE>. Add a
/// * try-finally block in your work methods around the command creations and
/// * enlist/delist the <CODE>XAResource</CODE> with the transaction. Use the
/// * protected <CODE>getTransaction</CODE> method to get a <seealso cref="XaTransaction"/>
/// * and add your commands to the transaction. Here is an example implementation:
/// * <p>
/// * 
/// * <pre>
/// * <CODE>
/// * public class MyConnection extends XaConnectionHelpImpl
/// * {
/// *     private static class MyResource extends XaResourceHelpImpl
/// *     {
/// *         public boolean isSameRM( XAResource rm )
/// *         {
/// *             if ( rm instanceof MyResource )
/// *             {
/// *                 return true;
/// *             }
/// *             return false;
/// *         }
/// *     }
/// * 
/// *     private XaResource xaResource = new MyResource();
/// * 
/// *     public XaResource getXaResource()
/// *     {
/// *         return xaResource;
/// *     }
/// * 
/// *     public void doWork1() throws SomeException
/// *     {
/// *         enlistResourceWithTx();
/// *         try
/// *         {
/// *             getTransaction().addCommand( new Work1Command( params ) );
/// *         }
/// *         finally
/// *         {
/// *             delistResourceFromTx();
/// *         }
/// *     }
/// * }
/// * </CODE>
/// * </pre> </summary>
/// 
	public abstract class XaConnectionHelpImpl : XaConnection
	{
		private readonly XaResourceManager xaRm;

		public XaConnectionHelpImpl(XaResourceManager xaRm)
		{
			if (xaRm == null)
			{
				throw new IllegalArgumentException("XaResourceManager is null");
			}
			this.xaRm = xaRm;
		}

///    
///     <summary> * Returns the XAResource associated with this connection.
///     *  </summary>
///     * <returns> The XAResource for this connection </returns>
///     
		public abstract XAResource getXaResource();

		public virtual void destroy()
		{
		// kill xaResource
			xaRm.destroy(getXaResource());
		}

///    
///     <summary> * Makes sure the resource is enlisted as active in the transaction.
///     *  </summary>
///     * <exception cref="XAException">
///     *             If resource not enlisted or suspended </exception>
///     
//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void validate() throws XAException
		public virtual void validate()
		{
			xaRm.validate(getXaResource());
		}

///    
///     <summary> * Returns the <seealso cref="XaTransaction"/> associated with this connection. If
///     * transaction is already completed it will still be returned.
///     *  </summary>
///     * <returns> The <seealso cref="XaTransaction"/> associated with this connection </returns>
///     * <exception cref="XAException">
///     *             If the transaction hasn't completed and the resource isn't
///     *             enlisted </exception>
///     
//JAVA TO VB & C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected XaTransaction getTransaction() throws XAException
		protected internal virtual XaTransaction getTransaction()
		{
			XAResource xar = getXaResource();
			XaTransaction xat = null;
			if (xar is XaResourceHelpImpl)
			{
				xat = ((XaResourceHelpImpl) xar).getCompletedTx();
			}
			if (xat != null)
			{
				return xat;
			}
			return xaRm.getXaTransaction(xar);
		}

///    
///     <summary> * Will clear the resource manager of all transactions. Used for testing
///     * purpose only. Do not use this method unless you know what you're doing
///     * since it will corrupt the state between the resource and the global
///     * transaction manager. </summary>
///     
		public virtual void clearAllTransactions()
		{
			xaRm.reset();
		}
	}
}